package com.pf.java;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.ListIterator;

/**
 * @author pf
 * @data 2021/10/28-20:22
 */
public class list {
    public static void main(String[] args) {
        List names = new ArrayList();
        names.add("Lily");
        names.add("Mary");
        names.add("John");
        names.add("Tony");
        names.add("Lisa");
        names.add("Mary");
        System.out.println(names);

        //插入
        names.add(1,"Lucy");
        System.out.println(names);

        names.remove(1);
        System.out.println(names);

        //遍历
        for (int i = 0; i < names.size(); i++) {
            System.out.println((i + 1) + " : " + names.get(i));
        }

        System.out.println(names.subList(2,4));
        System.out.println(names.indexOf("Mary") + " " + names.lastIndexOf("Mary"));

        ListIterator listIterator = names.listIterator();
        while (listIterator.hasNext()) {
            System.out.println(listIterator.next() + " ");
        }

        System.out.println("======");
        while (listIterator.hasPrevious()) {
            System.out.println(listIterator.previous());
        }

        System.out.println("=========");

        // 排序
        names.sort(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                String s1 = (String) o1;
                String s2 = (String) o2;
                if (s1 == null && s2 == null) {
                    return 0;
                } else if (s1 == null) {
                    return -1;
                } else if (s2 == null) {
                    return 1;
                } else {
                    return s1.compareTo(s2);
                }
            }
        });
        System.out.println(names);







    }

}
